package controller.day04;

import service.UserService;
import service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "DeleteUserServlet", value = "/DeleteUserServlet")
public class DeleteUserServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获得用户id
        String idStr = request.getParameter("id");
        Integer id = Integer.valueOf(idStr);

        //2. 调用service , 删除业务
        boolean result = userService.deleteUser(id);

        //3. 数据绑定  //4. 页面跳转
        if(result){
            request.setAttribute("msg","删除用户成功");
            request.getRequestDispatcher("/day04/success.jsp").forward(request,response);
        }else{
            request.setAttribute("msg","删除用户失败");
            request.getRequestDispatcher("/day04/allUser.jsp").forward(request,response);
        }
    }
}
